package com.framework.buss.ai.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.framework.buss.ai.constant.BussConstant;
import com.framework.buss.ai.mapper.DiseaseTypeMapper;
import com.framework.buss.ai.model.DiseaseType;
import com.framework.buss.ai.params.DiseaseTypeParam;
import com.framework.buss.ai.service.IDiseaseTypeService;
import com.framework.buss.ai.vo.DiseaseTypeVO;
import com.framework.common.exception.BusinessException;
import com.framework.common.util.BeanUtils;
import com.framework.common.util.Constant;
import com.framework.common.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author zhangzhg
 */
@Service
public class DiseaseTypeService implements IDiseaseTypeService {
    @Autowired
    private DiseaseTypeMapper diseaseTypeMapper;

    @Override
    public List<DiseaseType> list(DiseaseTypeParam param) {
        // 查询数据
        QueryWrapper<DiseaseType> queryWrapper = new QueryWrapper<>();
        // 前端展示，不同页面的层级不同
        queryWrapper
                .lambda()
                .eq(StringUtils.isNotBlank(param.getBridgeType()), DiseaseType::getBridgeType, param.getBridgeType())
                .eq(StringUtils.isNotBlank(param.getCategory()), DiseaseType::getCategory, param.getCategory())
                .eq(StringUtils.isNotBlank(param.getPid()), DiseaseType::getPid, param.getPid())
                .eq(StringUtils.isNotBlank(param.getType()), DiseaseType::getType, param.getType())
                .orderByAsc(DiseaseType::getSort);
        List<DiseaseType> list = diseaseTypeMapper.selectList(queryWrapper);
        return list;
    }

    @Override
    public List<DiseaseTypeVO> tree(DiseaseTypeParam param) {
        List<DiseaseType> list = diseaseTypeMapper.selectTree(param);
        return treeList(list, Constant.ROOT);
    }

    @Override
    public String save(DiseaseType param) {
        String id = param.getId();
        if (StringUtils.isBlank(id)) {
            String type = param.getType();
            // 不为全部，不可重复
            if (!BussConstant.TYPE_ALL.equals(type)) {
                QueryWrapper<DiseaseType> queryWrapper = new QueryWrapper<>();
                // 前端展示，不同页面的层级不同
                queryWrapper
                        .lambda()
                        .eq(StringUtils.isNotBlank(param.getBridgeType()), DiseaseType::getBridgeType, param.getBridgeType())
                        .eq(StringUtils.isNotBlank(param.getCategory()), DiseaseType::getCategory, param.getCategory())
                        .eq(StringUtils.isNotBlank(param.getType()), DiseaseType::getType, param.getType());
                boolean exists = diseaseTypeMapper.exists(queryWrapper);
                if (exists) {
                    throw new BusinessException("同一个专家下的类型值不能重复！");
                }
            }
            diseaseTypeMapper.insert(param);
        } else {
            diseaseTypeMapper.updateById(param);
        }

        return param.getId();
    }

    @Override
    public void del(List<String> id) {
        diseaseTypeMapper.deleteBatchIds(id);
    }

    @Override
    public DiseaseType getOne(String id) {
        return diseaseTypeMapper.selectById(id);
    }

    private List<DiseaseTypeVO> treeList(List<DiseaseType> list, String parentId) {
        return list.stream().filter(item -> item.getPid().equals(parentId)).map(item -> {
            DiseaseTypeVO vo = new DiseaseTypeVO();
            BeanUtils.copyProperties(item, vo);
            List<DiseaseTypeVO> childList = treeList(list, item.getId());
            if (childList != null && childList.size() > 0) {
                vo.setChildren(childList);
            }
            return vo;
        }).collect(Collectors.toList());
    }
}
    
